import Vue from 'vue'
import VueRouter from 'vue-router'
import hasBottomNav from '../views/hasBottomNav'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'hasBottomNav',
    component: hasBottomNav,
    redirect: '/home',
    children: [
      {
        path: 'home',
        name: 'Home',
        component: () => import ('../views/Home')
      },
      {
        path: 'types',
        name: 'types',
        component: () => import ('../views/types')
      },
      {
        path: 'user',
        name: 'user',
        component: () => import ('../views/user')
      }
    ]
  },{
    path: '/detail',
    name: 'Detail',
    meta: {
      isRequireLogin: true
    },
    component: () => import ('../views/Detail')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import ('../views/login')
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  
  if(to.matched.some(route => route.meta.isRequireLogin)) {
      var token = localStorage.getItem('token')
      if(!token) {
        next({
          path: '/login',
          query: to.query
        })
      } else {
        next()
      }
  } else {
    next()
  }
})
export default router
